﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Wys.DataForm.CSharp
{
    public partial class MenuHideIndex : Wys.Control.Vb.AutoAdjustFrom
    {
        public MenuHideIndex()
        {
            InitializeComponent();
        }

        private void btn关闭_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn保存_Click(object sender, EventArgs e)
        {
            if (dgMenu.Rows.Count > 0)
            {
                for (int i = 0; i < dgMenu.Rows.Count; i++)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("update XSCMenuBackUp Set ");

                    if (dgMenu.Rows[i].Cells["colHide"].Value.ToString() == "1")
                    {
                        sb.Append("IsHide =1");
                    }
                    else
                    {
                        sb.Append("IsHide =0");
                    }

                    if (dgMenu.Rows[i].Cells["colPassword"].Value.ToString() == "1")
                    {
                        sb.Append(",RequirePassword =1");
                    }
                    else
                    {
                        sb.Append(",RequirePassword =0");
                    }

                    sb.Append(",MenuIndex =" + (i + 1).ToString());
                    sb.Append(" Where ID=" + dgMenu.Rows[i].Cells["colId"].Value.ToString());

                    String sql = sb.ToString();
                    ct.ExecuteSql(sb.ToString());
                }
                RefreshTree(_selectNode);
            }
        }

        #region 加载子菜单
        private TreeNode _selectNode = null;
        private String _lastSelectNode = "";

        public void LoadNodeData(TreeNode tn, String strMenuName)
        {
            ADODB.Recordset rs = null;
            rs = ct.GetTable("SELECT * FROM  XSCMenuBackUp where ParentMenu='" + strMenuName + "' order by MenuIndex");
            if (rs != null)
            {
                while (!rs.EOF)
                {
                    TreeNode tnChild = new TreeNode();
                    tnChild.Text = rs.Fields["TITLE"].Value.ToString();
                    if (String.IsNullOrEmpty(rs.Fields["CLASSNAME"].Value.ToString()))
                    {
                        tnChild.Name = rs.Fields["MENUNAME"].Value.ToString();
                        tnChild.Tag = "parentmenu";
                        tn.Nodes.Add(tnChild);
                        LoadNodeData(tnChild, tnChild.Name);
                    }
                    else
                    {
                        //tnChild.Name = rs.Fields["CLASSNAME"].Value.ToString(); //D-2010-04-24
                        tnChild.Name = rs.Fields["ID"].Value.ToString();  //A-2010-04-24
                        tnChild.Tag = "child";
                        tn.Nodes.Add(tnChild);
                    }
                    if (tnChild.Name == _lastSelectNode)
                    {
                        _selectNode = tnChild;
                    }
                    rs.MoveNext();
                }
            }
            rs.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
        }
        #endregion

        #region 加载菜单
        public void LoadData()
        {
            _selectNode = null;
            this.trvRole.Nodes.Clear();
            ADODB.Recordset rs = ct.GetTable("SELECT * FROM  XSCMenuBackUp where ParentMenu is null order by MenuIndex");
            if (rs != null)
            {
                while (!rs.EOF)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = rs.Fields["TITLE"].Value.ToString();
                    tn.Name = rs.Fields["MENUNAME"].Value.ToString();
                    if (tn.Name == _lastSelectNode)
                    {
                        _selectNode = tn;
                    }
                    tn.Tag = "topmenu";
                    this.trvRole.Nodes.Add(tn);
                    rs.MoveNext();
                }
                rs.Close();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); 
                foreach (TreeNode tn in this.trvRole.Nodes)
                {
                    LoadNodeData(tn, tn.Name);
                }
            }
        }
        #endregion  

        private void RefreshTree(TreeNode e)
        {
            _selectNode = e;
            _lastSelectNode = e.Name;
            dgMenu.Rows.Clear();
            String strSql = "";

            if (e.Tag.ToString() == "topmenu")
            {
                ADODB.Recordset rs = ct.GetTable("SELECT * FROM  XSCMenuBackUp where ParentMenu is null order by MenuIndex");
                while (!rs.EOF)
                {
                    dgMenu.Rows.Add(new String[] { rs.Fields["ID"].Value.ToString(), rs.Fields["TITLE"].Value.ToString(), rs.Fields["IsHide"].Value.ToString(), rs.Fields["RequirePassword"].Value.ToString() });
                    rs.MoveNext();
                }
                rs.Close();

            }
            else if (e.Tag.ToString() == "parentmenu")
            {
                strSql = "select * from XSCMenuBackUp where  MENUNAME='" + e.Name + "'";
                DataTable dt = ct.FillTable(strSql);
                if (dt.Rows.Count > 0)
                {
                    ADODB.Recordset rs = ct.GetTable("SELECT * FROM  XSCMenuBackUp where ParentMenu ='" + dt.Rows[0]["ParentMenu"].ToString() + "' order by MenuIndex");
                    while (!rs.EOF)
                    {
                        dgMenu.Rows.Add(new String[] { rs.Fields["ID"].Value.ToString(), rs.Fields["TITLE"].Value.ToString(), rs.Fields["IsHide"].Value.ToString(), rs.Fields["RequirePassword"].Value.ToString() });
                        rs.MoveNext();
                    }
                    rs.Close();
                }
            }
            else if (e.Tag.ToString() == "child")
            {
                strSql = "select * from XSCMenuBackUp where  ID='" + e.Name + "'";
                DataTable dt = ct.FillTable(strSql);
                if (dt.Rows.Count > 0)
                {
                    ADODB.Recordset rs = ct.GetTable("SELECT * FROM  XSCMenuBackUp where ParentMenu ='" + dt.Rows[0]["ParentMenu"].ToString() + "' order by MenuIndex");
                    while (!rs.EOF)
                    {
                        dgMenu.Rows.Add(new String[] { rs.Fields["ID"].Value.ToString(), rs.Fields["TITLE"].Value.ToString(), rs.Fields["IsHide"].Value.ToString(), rs.Fields["RequirePassword"].Value.ToString() });
                        rs.MoveNext();
                    }
                    rs.Close();
                }
            }
        }
        private void trvRole_AfterSelect(object sender, TreeViewEventArgs e)
        {
            RefreshTree(e.Node);
        }

        private void MenuHideIndex_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            try
            {
                int rowIndex = dgMenu.SelectedRows[0].Index;  //得到当前选中行的索引      

                if (rowIndex == 0)
                {
                    return;
                }

                List<string> list = new List<string>();
                for (int i = 0; i < dgMenu.Columns.Count; i++)
                {
                    list.Add(dgMenu.SelectedRows[0].Cells[i].Value.ToString());   //把当前选中行的数据存入list数组中      
                }

                for (int j = 0; j < dgMenu.Columns.Count; j++)
                {
                    dgMenu.Rows[rowIndex].Cells[j].Value = dgMenu.Rows[rowIndex - 1].Cells[j].Value;
                    dgMenu.Rows[rowIndex - 1].Cells[j].Value = list[j].ToString();
                }
                dgMenu.Rows[rowIndex].Selected = false;  
                dgMenu.Rows[rowIndex - 1].Selected = true;
            }
            catch (Exception)
            {
                
            }
            
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            try
            {
                int rowIndex = dgMenu.SelectedRows[0].Index;  //得到当前选中行的索引      

                if (rowIndex == dgMenu.Rows.Count - 1)
                {
                    return;
                }

                List<string> list = new List<string>();
                for (int i = 0; i < dgMenu.Columns.Count; i++)
                {
                    list.Add(dgMenu.SelectedRows[0].Cells[i].Value.ToString());   //把当前选中行的数据存入list数组中      
                }

                for (int j = 0; j < dgMenu.Columns.Count; j++)
                {
                    dgMenu.Rows[rowIndex].Cells[j].Value = dgMenu.Rows[rowIndex + 1].Cells[j].Value;
                    dgMenu.Rows[rowIndex + 1].Cells[j].Value = list[j].ToString();
                }
                dgMenu.Rows[rowIndex].Selected = false;
                dgMenu.Rows[rowIndex + 1].Selected = true;
            }
            catch (Exception)
            {

            }
            
        }
    }
}
